/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ambimmort.ucagent.database;

import cn.net.greenet.db.HibernateUtil;

import cn.net.greenet.db.bean.Systemproperty;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author 定巍
 */
public class SystemPropertyUtil {

    private static SystemPropertyUtil instance = null;

    private SystemPropertyUtil() {

        init();
    }

    private void init() {

        if (!hasProperty("server.port")) {
            newProperty("server.port", "50000");
        }
        if (!hasProperty("server.throttle")) {
            newProperty("server.throttle", "1000");
        }
        if (!hasProperty("zmq.server.url")) {
            newProperty("zmq.server.url", "tcp://localhost:12590");
//            newProperty("zmq.server.url", "tcp://10.10.2.39:12590");
        }
        if (!hasProperty("mongodb.ip")) {
            newProperty("mongodb.ip", "localhost");
        }
        if (!hasProperty("aaa.td.freq")) {
            newProperty("aaa.td.freq", "300000");
        }
//        newProperty("PolicyRepoServer.webserver.url", "http://localhost:9004");
    }

    public static SystemPropertyUtil getInstance() {
        if (instance == null) {
            instance = new SystemPropertyUtil();
        }
        return instance;
    }

    public boolean hasProperty(String key) {
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = s.beginTransaction();
        Object obj = s.createQuery("select p FROM Systemproperty p where p.property='" + key + "'").uniqueResult();
        tx.commit();
        s.close();
        return obj != null;
    }

    public void setProperty(String key, String value) {
        Systemproperty bean = new Systemproperty(key, value);
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = s.beginTransaction();
        try {
            s.saveOrUpdate(bean);
            tx.commit();

        } catch (Exception e) {
            tx.rollback();
        } finally {
            s.close();
        }
    }

    public void newProperty(String key, String value) {
        Systemproperty bean = new Systemproperty(key, value);
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = s.beginTransaction();
        try {
            s.saveOrUpdate(bean);
            tx.commit();

        } catch (Exception e) {
            tx.rollback();
        } finally {
            s.close();
        }

    }

    public String getProperty(String key) {
        Session s = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = s.beginTransaction();
        Object obj = s.createQuery("select p FROM Systemproperty p where p.property='" + key + "'").uniqueResult();
        tx.commit();
        if (obj != null) {
            Systemproperty p = (Systemproperty) obj;
            return p.getValue();
        }
        s.close();
        return null;
    }
}
